<?php

namespace App\Api;

use PhalApi\Api;


/**
 * 默认读取，更新，删除
 *
 * @author: dogstar <chanzonghuang@gmail.com> 2014-10-04
 */
class Index extends Api {

	public function getRules() {
		return array(
			'select'       => array(
				'page'  => array( 'name' => 'page', 'require' => true,  'desc' => '当前页' ),
				'size'  => array( 'name' => 'size', 'require' => true,  'desc' => '每页显示多少' ),
				'table' => array( 'name' => 'table', 'require' => false, 'defualt'=>'rok_account',  'desc' => '表名' ),
			),
			"update"       => array(
				'id'    => array( 'name' => 'id', 'require' => false, 'desc' => 'id' ),
				'arr'   => array( 'name' => 'arr', 'require' => true, 'desc' => '数据json数据' ),
				'table' => array( 'name' => 'table', 'require' => true, 'desc' => '表名' ),
			),
			"delete"       => array(
				'id'    => array( 'name' => 'id', 'require' => false, 'desc' => 'id' ),
				'table' => array( 'name' => 'table', 'require' => true, 'desc' => '表名' ),
			),
			"select_where" => array(
				'where' => array( 'name' => 'where', 'require' => true, 'desc' => '查询条件' ),
				'table' => array( 'name' => 'table', 'require' => true, 'desc' => '表名' ),
				'page'  => array( 'name' => 'page', 'require' => true, 'desc' => '当前页' ),
				'size'  => array( 'name' => 'size', 'require' => true, 'desc' => '每页显示多少' ),
				'order'  => array( 'name' => 'order', 'require' => false, 'desc' => '排序' ),
			),
           "login" => array(
               'username' => array( 'name' => 'username',  'desc' => 'username' ),
               'password' => array( 'name' => 'password',  'desc' => 'password' ),
           ),
            "userInfo" => array(
                'username' => array( 'name' => 'username',  'desc' => 'username' ),
                'password' => array( 'name' => 'password',  'desc' => 'password' ),
                'tokenName' => array( 'name' => 'tokenName',  'desc' => 'password' ),
            ),
            "logout" => array()
		);
	}

    /**
     * 通用多条件查询接口
     * @desc 多条件查询接口
     * @return array data 被查数据
     * @exception 400 非法请求，参数传递错误
     */
    public function logout() {
        header( 'Access-Control-Allow-Origin:*' );
        \PhalApi\DI()->response->setMsg('success');
        return '';
    }

    /**
     * 通用多条件查询接口
     * @desc 多条件查询接口
     * @return array data 被查数据
     * @exception 400 非法请求，参数传递错误
     */
    public function userInfo() {
        header( 'Access-Control-Allow-Origin:*' );
        \PhalApi\DI()->response->setMsg('success');

        $res = array(
            "permissions"=> array("admin"),
            "username"=> $this->tokenName,
            "avatar"=> "",
            "roles" => array("admin")
        );

        return $res;
    }

    /**
     * 通用多条件查询接口
     * @desc 多条件查询接口
     * @return array data 被查数据
     * @exception 400 非法请求，参数传递错误
     */
    public function login() {
        header( 'Access-Control-Allow-Origin:*' );
        $res = array(
            "accessToken"=> $this->username
        );
        \PhalApi\DI()->response->setMsg('success');
        return $res;
    }


    /**
	 * 通用多条件查询接口
	 * @desc 多条件查询接口
	 * @return array data 被查数据
	 * @exception 400 非法请求，参数传递错误
	 */
	public function select_where() {
		header( 'Access-Control-Allow-Origin:*' );
		return \App\getInfoByPageBywhere( $this );
	}

	/**
	 * 删除接口
	 * @desc 更新id删除接口
	 * @return array data 删除状态
	 * @exception 400 非法请求，参数传递错误
	 */
	public function delete() {
		header( 'Access-Control-Allow-Origin:*' );
		return \App\deleteById( $this );
	}

	/**
	 * 更新及创建->接口
	 * @desc 更新及新建接口服务
	 * @return array data 数据内容
	 * @return int count 当前条件下总数
	 * @exception 400 非法请求，参数传递错误
	 */
	public function update() {
		header( 'Access-Control-Allow-Origin:*' );
		return \App\updateById( $this );
	}

	/**
	 * 分页读取
	 * @desc 按分页读取数据接口服务
	 * @return array data 数据内容
	 * @return int count 当前条件下总数
	 * @exception 400 非法请求，参数传递错误
	 */
	public function select() {
    // 最简单的处理方式
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
    header('Access-Control-Allow-Headers: *');

//        header('Access-Control-Allow-Headers:x-requested-with,content-type,requesttype');
		return \App\getInfoByPage( $this );
	}

}
